package de.tayrracer;

import de.tayrracer.geoobjects.*;
import de.tayrracer.geoobjects.BaseObject.Ray;

import java.awt.*;
import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PictureDrawer {
	private BaseObject[] theObjects = new BaseObject[1];
	private Graphics canvas = null;
	final Lock mutex = new ReentrantLock();
	public final int threadCount = 8;
	public int completeCount = 0;
	
	
	public PictureDrawer(Graphics theCanvas){
		canvas = theCanvas;
		/*java.util.Random random = new java.util.Random();
		for(int i = 2; i < theObjects.length;i++){
			theObjects[i] = new Sphere(new double[]{-100+random.nextInt(200),-100+random.nextInt(200), 100+random.nextInt(200)}, 20);
			theObjects[i].setObjectColor(Color.getHSBColor(random.nextFloat(),random.nextFloat(),random.nextFloat()));
			theObjects[i].setMirrorPercentage(0.5);
		}
		*/
		theObjects[0] = new Triangle(new double[]{-10,0,200},new double[]{-10,-5,20},new double[]{10,-5,20});
		theObjects[0].setObjectColor(Color.RED);
		theObjects[1] = new Triangle(new double[]{10,-5,20},new double[]{-10,0,200},new double[]{10,0,200});
		theObjects[1].setObjectColor(Color.BLUE);
		
		//theObjects[0] = new Mesh("/Users/speckwolf/Downloads/teapot.obj",new double[]{-100,-3,200});
		
		theObjects[0] = new Sphere(new double[]{0, 0, 50}, 10);
		theObjects[0].setObjectColor(Color.GREEN);
		//theObjects[1] = new Sphere(new double[]{10, 0, 60}, 10);
		//theObjects[1].setObjectColor(Color.RED);*/
	}
	
	public void raytrace(){
		final int threadHeight = 600/threadCount;
		ArrayList<Thread> myThreads = new ArrayList<Thread>();
		for(int i = 0; i<threadCount; i++){
			Thread tr = new TayRace(threadHeight*i,threadHeight*(i+1)-1);
			tr.start();
			myThreads.add(tr);
		}
	}
	
	public static Color getFirstCut(BaseObject[] theObjects, double[] origin, double[] direction, double distance){
		Color result = Color.BLACK;
		for(int k=0; k < theObjects.length; k++){
			Ray tempRay = theObjects[k].getColorForCut(origin, direction, theObjects, 5);
			if(tempRay != null){
				double tempDist = de.tayrracer.Math.distance(tempRay.position, origin); 
				if(tempDist< distance){
					distance = tempDist;
					float[] rgbArr = Color.RGBtoHSB(tempRay.rayColor.getRed(), tempRay.rayColor.getGreen(), tempRay.rayColor.getBlue(), null);
					double theValue = 1 - (distance)/200;
					result = Color.getHSBColor(rgbArr[0], rgbArr[1], (float)(theValue));
				}
			}
		}
		return result;
	}
	
	private class TayRace extends Thread {
		private final int beginY;
		private final int endY;
		public TayRace(int begin, int end){
			beginY = begin;
			endY = end;
		}
		
		@Override
		
		public void run(){
			if(canvas != null){
				double[] origin = {0,0,0};
				double[] direction = {0,0,1};
				double distance = Double.POSITIVE_INFINITY;
				for(int i = 0; i<800;i++){
					direction[0] = -2.0/3+i/800.0*4.0/3;
					for(int j = beginY; j<=endY;j++){
						distance = Double.POSITIVE_INFINITY;
						direction[1] = 1.0/2-j/600.0;
						Color theColor = Color.BLUE;

						theColor = getFirstCut(theObjects, origin, direction, distance);
						
						mutex.lock();
						canvas.setColor(theColor);
						canvas.drawLine(i, j, i, j);
						mutex.unlock();
					}
				}
			}
			completeCount++;
		}
	}
}